#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@NAME          : fileapi.py
@TIME          : 2024/11/20 19:50:27
@AUTHOR        : chenlip
@VERSION       : 0.0.1
@DESCRIPTION   : Minio图片浏览的蓝图 路由程序
'''
# --------------------------引用--------------------------
from flask import Blueprint, render_template, current_app, url_for
from flask import request
from app.common.mcimgview import getMCDirTree, getPicList, getMvList
from app.common.utils import Utils
# --------------------------引用--------------------------

mcpicview = Blueprint('mcpicview', __name__)

@mcpicview.route('/miniores/mcpicview', methods=['GET', 'POST'])
def mcpicshow():
    # 获取minio的images目录树
    _mcdir = "images/" # 默认目录
    _mcbucketname = "static" # 默认bucket

    # 从前端form表单(input id="srcdir")获取目录路径
    if request.method == 'POST':
        
        _mcdir = request.form.get('srcdir', type=str)
        _mcbucketname = request.form.get('bucketname', type=str)
        print("request.form == ", _mcdir, _mcbucketname)
    
    if _mcbucketname == "static":
        _mc = current_app.config['MINIO']
    else:
        # 51epub.com/mdimages/bg/wallsrc/
        _mc = current_app.config['MINIO2']

    _mcinfo = {"_mcdir": _mcdir, "_mcbucketname": _mcbucketname}

    # 获取目录树
    _ulhtml = getMCDirTree(_mcdir, _mc)
    
    _renderhtml = render_template('mediaview/miopicview.html', ulhtml=_ulhtml, mcinfo=_mcinfo)

    return _renderhtml

@mcpicview.route('/miniores/mcpiclist', methods=['GET'])
def getpiclist():
    # 获取前端传递的body参数（picpath,page）
    _picpath = request.args.get('picpath')
    _pagenum = request.args.get('page', default=1, type=int)

    try:
        _piclist = current_app.config[_picpath]
    except KeyError:
        _piclist = None

    if not _piclist:
        if not _picpath.startswith("images/"):
            _mc = current_app.config['MINIO2']
        else:
            _mc = current_app.config['MINIO']

        _piclist = getPicList(_picpath, _mc)
        # 使用current_app将piclist保存到全局变量中
        current_app.config[_picpath] = _piclist

    # 对列表分页
    _piclist = Utils.list_pagination(_piclist, _pagenum, 20, _picpath)
    _mcimgs = _piclist[0]
    _pagehtml = _piclist[3]

    _renderhtml = render_template('mediaview/piccardlist.html', mcimgs=_mcimgs, pagehtml=_pagehtml)

    return _renderhtml

@mcpicview.route('/miniores/mcmvview', methods=['GET', 'POST'])
def mcmvshow():
    # 获取minio的video目录树
    _mcdir = "video/"
    _mc = current_app.config['MINIO']
    _ulhtml = getMCDirTree(_mcdir, _mc)
    
    _renderhtml = render_template('mediaview/miomvview.html', ulhtml=_ulhtml)
    return _renderhtml

@mcpicview.route('/miniores/mcmvlist', methods=['GET'])
def getmvlist():

    # 获取前端传递的body参数（picpath,page）
    _mvpath = request.args.get('mvpath')
    _pagenum = request.args.get('page', default=1, type=int)

    try:
        _mvlist = current_app.config[_mvpath]
    except KeyError:
        _mvlist = None

    if not _mvlist:
        _mc = current_app.config['MINIO']
        _mvlist = getMvList(_mvpath, _mc)
        # 使用current_app将piclist保存到全局变量中
        current_app.config[_mvpath] = _mvlist

    # 对列表分页
    _mvlist = Utils.list_pagination(_mvlist, _pagenum, 20, _mvpath)
    _mcmvs = _mvlist[0]
    _pagehtml = _mvlist[3]

    _renderhtml = render_template('mediaview/mvcardlist.html', mcmvs=_mcmvs, pagehtml=_pagehtml)

    return _renderhtml